package qky.api.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public abstract class BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

    protected static final String PAGE_SIZE = "20";

    public Map<String, Object> buildPageParam(Map<String, Object> paramMap) {
        Map<String, Object> searchParams = new HashMap<>();

        for (String s : paramMap.keySet()) {
            if(s.startsWith("search_")){
                searchParams.put(s.substring(7), paramMap.get(s));
            }
        }

        int pageNum = (paramMap.containsKey("pageIndex") ? (int) paramMap.get("pageIndex") : 0) + 1;
        int size = (paramMap.containsKey("pageSize") ? (int) paramMap.get("pageSize") : Integer.valueOf(PAGE_SIZE));
        // int pageNum = (offset / size) + 1;

        searchParams.put("pageNum", pageNum);
        searchParams.put("size", size);
        if(paramMap.containsKey("order")) searchParams.put("order", paramMap.get("order"));
        if(paramMap.containsKey("sort")) searchParams.put("sort", paramMap.get("sort"));
        return searchParams;
    }

}
